﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProductManagement.Applcation.Command;
using ProductManagement.Domain.Dtos;
using ProductManagement.Domain.ProductsDomain;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProductManagement.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class ProductTypeController : ControllerBase
    {
        protected readonly IMediator mediatorp;

        public ProductTypeController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }

        /// <summary>
        /// 产品分类添加
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductTypeAdd(ProductTypeCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.AddDtos.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.AddDtos.AddName = name;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                cd.AddDtos.User_Id = int.Parse(userId);
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 产品分类显示树形
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ProductsTypeSelectDto>>> ProductTypeSelect([FromQuery] ProductsTypeSelectCommand command)
        {
            ApiResult<List<ProductsTypeSelectDto>> result = new ApiResult<List<ProductsTypeSelectDto>>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 产品分类点击反填
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ProductsTypeSetDto>>> ProductTypeSet([FromQuery] ProductsTypeSetCommand command)
        {
            ApiResult<List<ProductsTypeSetDto>> result = new ApiResult<List<ProductsTypeSetDto>>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 产品分类批量删除
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductTypeDelete(ProductTypeDeleteCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.DelName = name;
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 产品分类反填
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ProductsTypeAssignmentDto>> ProductTypeAssignment([FromQuery] ProductsTypeAssignmentCommand command)
        {
            ApiResult<ProductsTypeAssignmentDto> result = new ApiResult<ProductsTypeAssignmentDto>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 产品分类修改
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> ProductTypeUpdate(ProductsTypeUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.dtos.Office_Id = sid;             
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                command.dtos.User_Id = int.Parse(userId);
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                command.dtos.UpdName = name;
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
    }
}
